package com.itheima.service;

import com.itheima.pojo.Account;
import com.itheima.pojo.Arrangement;
import com.itheima.pojo.Movie;
import com.itheima.pojo.ShowMovie;

import java.util.ArrayList;
import java.util.Scanner;

public class Client {
    Scanner sc = new Scanner(System.in);
    Account account;
    ArrayList<Arrangement> arrayList;


    /**
     * 1.客户端的操作页/主页方法
     */
    public void start(Account account, ArrayList<ShowMovie> showMovies, ArrayList<Arrangement> arrayList,ArrayList<Movie> list) {
        this.account = account;
        this.arrayList = arrayList;

        //1.1 重复进行功能选择a
        while (true) {
            System.out.println("==================黑马影院==================");
            System.out.println("尊敬的用户" + account.getUsername() + "，欢迎使用黑马影院！");
            System.out.println("请选择以下功能：\r\n1.热门影片\t2.即将上映\t3.票夹\t4.我的\t5.退出");
            //1.2 使用Scanner录入选择
            String choice = sc.next();

            //1.3 根据选择的不同执行不同功能：switch分支结构
            switch (choice) {
                case "1" -> hotMovie(showMovies);
                case "2" -> jiJiang(list);
                case "3" -> System.out.println("3");
                case "4" -> System.out.println("4");
                case "5" -> {
                    System.out.println("感谢您的使用，再见！");
                    return;
                }
                default -> System.out.println("您的选择有误，请检查后重试！");
            }
        }

    }

    //  热门影片
    public void hotMovie(ArrayList<ShowMovie> showMovies) {
        int count = 1;
        if (showMovies.size() == 0) {
            System.out.println("尚无电影上映,请过阵子再来哦!");
            return;
        }
        for (int i = 0; i < showMovies.size(); i++) {
            if (count == showMovies.size()) {
                System.out.println(showMovies.get(i));
            } else {
                System.out.println(showMovies.get(i));
                count++;
                System.out.println("---------------------");
            }
        }

        System.out.println("是否购票?1.是\t2.否");
        String next = sc.next();
        while (true) {
            if (next.equals("1")) {

                buyMovie();

                return;
            } else if (next.equals("2")) {
                System.out.println("欢迎您再次使用!");
                return;
            } else {
                System.out.println("您输入的信息有误,请重新输入!");
            }
        }
    }


    public void buyMovie() {
        int count = 0;
        System.out.println("===============电影购票==============");
        System.out.println("请输入电影名称:");
        String s = sc.next();

        for (Arrangement arrangement : arrayList) {
            if (s.equals(arrangement.getMovieName())) {
                System.out.println("=======================" + arrangement.getNumber() + "号厅====================");
                System.out.print("开始时间:" + arrangement.getStartTime() + "\t\t");
                System.out.print("结束时间:" + arrangement.getEndTime() + "\t\t");
                System.out.print("价格:" + arrangement.getPrice() + "\t\t");
                System.out.println("已售:" + arrangement.getSold());
                System.out.println("座位:");

                for (int i = 0; i < arrangement.table.length; i++) {
                    System.out.print(i + 1 + "排" + "\t\t");
                    for (int j = 0; j < arrangement.table[i].length; j++) {
                        if (arrangement.table[i][j] == 1) {
                            System.out.print(j + 1 + ":[*]\t");
                        } else {
                            System.out.print(j + 1 + ":[ ]\t");
                        }
                    }
                    System.out.println();
                }
                System.out.println("是否选座?1.是\t2.否");
                String next = sc.next();
                if ("1".equals(next)) {
                    seat(arrangement);
                    return;
                } else if ("2".equals(next)) {
                    return;
                }
            } else {
                count++;
            }

            if (count == arrayList.size()) {
                System.out.println("很抱歉,未查询到该电影的信息,请确认后重试!");
                return;
            }
        }
    }

    public void seat(Arrangement arrangement) {
        int i = 0;
        int i1 = 0;
        while (true) {
            System.out.println("===========选座购票============");
            System.out.println("请输入排数:");

            while (true) {
                i = sc.nextInt();
                if (i >= 1 && i <= 11) {
                    break;
                } else {
                    System.out.println("很抱歉,排数必须在1到10中选择!");
                }
            }
            System.out.println("请输入列数:");
            while (true) {
                i1 = sc.nextInt();
                if (i1 >= 1 && i1 <= 18) {

                    break;
                } else {
                    System.out.println("很抱歉,列数必须在1到10中选择!");
                }
            }

            if(arrangement.table[i-1][i1-1]==1){
                System.out.println("很抱歉,该位置已被购买,购票失败!请问是否继续?");
                System.out.println("1.是\t\t2.否");
                String next = sc.next();
                if("2".equals(next)){
                    return;
                }else if("1".equals(next)){
                }
            }else {
                break;
            }
        }

        System.out.println("购票成功!您的座位是" + i + "排" + i1 + "列");
        arrangement.table[i - 1][i1 - 1] = 1;
        arrangement.setSold(arrangement.getSold() + 1);


    }


//    即将上映
    public void jiJiang(ArrayList<Movie> list){
        ArrayList<Movie> movie = new ArrayList<>();
        System.out.println("================即将上映=============");

        for (Movie movie1 : list) {
            int count = 0;
            for (Arrangement arrangement : arrayList) {
                if(movie1.getName().equals(arrangement.getMovieName())){
                    break;
                }else {
                    count++;
                }

                if(count==arrayList.size()){
                    movie.add(movie1);
                }
            }

        }

        for (Movie movie1 : movie) {
            System.out.println(movie1);
        }
    }


}
